package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarRangeWaterTemp;
import com.navinfo.platform.trip.common.arithmetic.enums.RangeWaterTempEnum;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 发动机水温区间统计<br/>
 * 依赖{@link IdleParking}的怠速状态<br/>
 * 输出：统计
 * @author 沈东生
 */
public class RangeWaterTemp extends AbstractArithmetic<VarRangeWaterTemp> {
    @Override
    public void calculate() {
        logger.debug("开始发动机水温区间统计算法[terminalId:{}]", terminalId);
        CommonData commonData = getCommonData();
        Integer waterTem = commonData.getWaterTemp();
        if (waterTem == null) {
            return;
        }

        //水温有效性校验
        if (!RangeWaterTempEnum.RANGE_VALID.getRange().contains(waterTem)) {
            return;
        }

        VarRangeWaterTemp var = getObj();
        boolean work = commonData.isWork();
        int duration = commonData.getDffTime();
        if (RangeWaterTempEnum.RANGE_50.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp50Duration(
                        Optional.ofNullable(var.getWorkWaterTemp50Duration()).orElse(0) + duration);
            } else {
                var.setIdleWaterTemp50Duration(
                        Optional.ofNullable(var.getIdleWaterTemp50Duration()).orElse(0) + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_70.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp70Duration(
                        Optional.ofNullable(var.getWorkWaterTemp70Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTemp70Duration(
                        Optional.ofNullable(var.getIdleWaterTemp70Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_80.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp80Duration(
                        Optional.ofNullable(var.getWorkWaterTemp80Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTemp80Duration(
                        Optional.ofNullable(var.getIdleWaterTemp80Duration()).orElse(0) + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_85.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp85Duration(
                        Optional.ofNullable(var.getWorkWaterTemp85Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTemp85Duration(
                        Optional.ofNullable(var.getIdleWaterTemp85Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_90.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp90Duration(
                        Optional.ofNullable(var.getWorkWaterTemp90Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTemp90Duration(
                        Optional.ofNullable(var.getIdleWaterTemp90Duration()).orElse(0) + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_95.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp95Duration(
                        Optional.ofNullable(var.getWorkWaterTemp95Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTemp95Duration(
                        Optional.ofNullable(var.getIdleWaterTemp95Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_100.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp100Duration(
                        Optional.ofNullable(var.getWorkWaterTemp100Duration()).orElse(0)   + duration);
            } else {
                var.setIdleWaterTemp100Duration(
                        Optional.ofNullable(var.getIdleWaterTemp100Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_105.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp105Duration(
                        Optional.ofNullable(var.getWorkWaterTemp105Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTemp105Duration(
                        Optional.ofNullable(var.getIdleWaterTemp105Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_110.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp110Duration(
                        Optional.ofNullable(var.getWorkWaterTemp110Duration()).orElse(0)   + duration);
            } else {
                var.setIdleWaterTemp110Duration(
                        Optional.ofNullable(var.getIdleWaterTemp110Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_115.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp115Duration(
                        Optional.ofNullable(var.getWorkWaterTemp115Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTemp115Duration(
                        Optional.ofNullable(var.getIdleWaterTemp115Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_120.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp120Duration(
                        Optional.ofNullable(var.getWorkWaterTemp120Duration()).orElse(0) + duration);
            } else {
                var.setIdleWaterTemp120Duration(
                        Optional.ofNullable(var.getIdleWaterTemp120Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_125.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp125Duration(
                        Optional.ofNullable(var.getWorkWaterTemp125Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTemp125Duration(
                        Optional.ofNullable(var.getIdleWaterTemp125Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_130.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp130Duration(
                        Optional.ofNullable(var.getWorkWaterTemp130Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTemp130Duration(
                        Optional.ofNullable(var.getIdleWaterTemp130Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_135.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp135Duration(
                        Optional.ofNullable(var.getWorkWaterTemp135Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTemp135Duration(
                        Optional.ofNullable(var.getIdleWaterTemp135Duration()).orElse(0)  + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_140.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp140Duration(
                        Optional.ofNullable(var.getWorkWaterTemp140Duration()).orElse(0)
                                + duration);
            } else {
                var.setIdleWaterTemp140Duration(
                        Optional.ofNullable(var.getIdleWaterTemp140Duration()).orElse(0)
                                + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_145.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp145Duration(
                        Optional.ofNullable(var.getWorkWaterTemp145Duration()).orElse(0)
                                + duration);
            } else {
                var.setIdleWaterTemp145Duration(
                        Optional.ofNullable(var.getIdleWaterTemp145Duration()).orElse(0)
                                + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_150.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTemp150Duration(
                        Optional.ofNullable(var.getWorkWaterTemp150Duration()).orElse(0)
                                + duration);
            } else {
                var.setIdleWaterTemp150Duration(
                        Optional.ofNullable(var.getIdleWaterTemp150Duration()).orElse(0)
                                + duration);
            }
        } else if (RangeWaterTempEnum.RANGE_OVER_150.getRange().contains(waterTem)) {
            if (work) {
                var.setWorkWaterTempOver150Duration(
                        Optional.ofNullable(var.getWorkWaterTempOver150Duration()).orElse(0)  + duration);
            } else {
                var.setIdleWaterTempOver150Duration(
                        Optional.ofNullable(var.getIdleWaterTempOver150Duration()).orElse(0) + duration);
            }
        }

        if (work) {
            if (var.getMaxWorkWaterTemp() == null || waterTem > var.getMaxWorkWaterTemp()) {
                var.setMaxWorkWaterTemp(waterTem);
            }
            if (var.getMinWorkWaterTemp() == null || waterTem < var.getMinWorkWaterTemp()) {
                var.setMinWorkWaterTemp(waterTem);
            }
            var.setWorkWaterTempSum(Optional.ofNullable(var.getWorkWaterTempSum()).orElse(0L) + waterTem);
            var.setWorkWaterTempCount(Optional.ofNullable(var.getWorkWaterTempCount()).orElse(0) + 1);
        } else {
            if (var.getMaxIdleWaterTemp() == null || waterTem > var.getMaxIdleWaterTemp()) {
                var.setMaxIdleWaterTemp(waterTem);
            }
            if (var.getMinIdleWaterTemp() == null || waterTem < var.getMinIdleWaterTemp()) {
                var.setMinIdleWaterTemp(waterTem);
            }
            var.setIdleWaterTempSum(Optional.ofNullable(var.getIdleWaterTempSum()).orElse(0L) + waterTem);
            var.setIdleWaterTempCount(Optional.ofNullable(var.getIdleWaterTempCount()).orElse(0) + 1);
        }

        logger.debug("结束发动机水温区间统计算法[terminalId:{}]", terminalId);
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarRangeWaterTemp var=getObj();

        //workWaterTemp
        statisticDataOut.setWorkWaterTemp50Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp50Duration()));
        statisticDataOut.setWorkWaterTemp70Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp70Duration()));
        statisticDataOut.setWorkWaterTemp80Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp80Duration()));
        statisticDataOut.setWorkWaterTemp85Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp85Duration()));
        statisticDataOut.setWorkWaterTemp90Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp90Duration()));
        statisticDataOut.setWorkWaterTemp95Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp95Duration()));
        statisticDataOut.setWorkWaterTemp100Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp100Duration()));
        statisticDataOut.setWorkWaterTemp105Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp105Duration()));
        statisticDataOut.setWorkWaterTemp110Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp110Duration()));
        statisticDataOut.setWorkWaterTemp115Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp115Duration()));
        statisticDataOut.setWorkWaterTemp120Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp120Duration()));
        statisticDataOut.setWorkWaterTemp125Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp125Duration()));
        statisticDataOut.setWorkWaterTemp130Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp130Duration()));
        statisticDataOut.setWorkWaterTemp135Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp135Duration()));
        statisticDataOut.setWorkWaterTemp140Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp140Duration()));
        statisticDataOut.setWorkWaterTemp145Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp145Duration()));
        statisticDataOut.setWorkWaterTemp150Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTemp150Duration()));
        statisticDataOut.setWorkWaterTempOver150Duration(ArithmeticUtils.timeMsToSecond(var.getWorkWaterTempOver150Duration()));

        //idleWaterTemp
        statisticDataOut.setIdleWaterTemp50Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp50Duration()));
        statisticDataOut.setIdleWaterTemp70Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp70Duration()));
        statisticDataOut.setIdleWaterTemp80Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp80Duration()));
        statisticDataOut.setIdleWaterTemp85Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp85Duration()));
        statisticDataOut.setIdleWaterTemp90Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp90Duration()));
        statisticDataOut.setIdleWaterTemp95Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp95Duration()));
        statisticDataOut.setIdleWaterTemp100Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp100Duration()));
        statisticDataOut.setIdleWaterTemp105Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp105Duration()));
        statisticDataOut.setIdleWaterTemp110Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp110Duration()));
        statisticDataOut.setIdleWaterTemp115Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp115Duration()));
        statisticDataOut.setIdleWaterTemp120Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp120Duration()));
        statisticDataOut.setIdleWaterTemp125Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp125Duration()));
        statisticDataOut.setIdleWaterTemp130Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp130Duration()));
        statisticDataOut.setIdleWaterTemp135Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp135Duration()));
        statisticDataOut.setIdleWaterTemp140Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp140Duration()));
        statisticDataOut.setIdleWaterTemp145Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp145Duration()));
        statisticDataOut.setIdleWaterTemp150Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTemp150Duration()));
        statisticDataOut.setIdleWaterTempOver150Duration(ArithmeticUtils.timeMsToSecond(var.getIdleWaterTempOver150Duration()));

        statisticDataOut.setWorkWaterTempSum(var.getWorkWaterTempSum() );
        statisticDataOut.setWorkWaterTempCount(var.getWorkWaterTempCount() );

        statisticDataOut.setIdleWaterTempSum(var.getIdleWaterTempSum() );
        statisticDataOut.setIdleWaterTempCount(var.getIdleWaterTempCount() );

        statisticDataOut.setMinWorkWaterTemp(var.getMinWorkWaterTemp());
        statisticDataOut.setMaxWorkWaterTemp(var.getMaxWorkWaterTemp());

        statisticDataOut.setMinIdleWaterTemp(var.getMinIdleWaterTemp());
        statisticDataOut.setMaxIdleWaterTemp(var.getMaxIdleWaterTemp());
    }
}
